package com.lca.lca_yuekao.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lca.lca_yuekao.entity.User;
import com.lca.lca_yuekao.service.UserService;
import com.lca.lca_yuekao.vo.SearchVo;
import org.springboot.bw.struct.PageResult;
import org.springboot.bw.struct.Result;
import org.springboot.bw.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 关注公众号：lca
 * @since 2022-07-12
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService userService;

    @PostMapping("search/{currentpage}/{pagesize}")
    public PageResult search(@PathVariable("currentpage") Integer currentpage,
                             @PathVariable("pagesize") Integer pagesize,
                             @RequestBody(required = false)SearchVo vo){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (Objects.nonNull(vo)){
            if (StringUtils.isNotEmpty(vo.getName())){
                queryWrapper.like("name",vo.getName());
            }
        }

        Page<User> tpage = new Page<>(currentpage, pagesize);
        Page<User> page = this.userService.page(tpage, queryWrapper);
        PageResult<User> of = PageResult.of(page.getRecords(), page.getTotal());
        return of;
    }

    @PostMapping("add")
    public Result add(@RequestBody() User user){
        boolean save = this.userService.save(user);
        return save?Result.ok():Result.error();
    }

}
